package com.struts2.service;

import java.util.List;








import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.struts2.model.Category;
import com.struts2.model.Color;
import com.struts2.model.Customer;
import com.struts2.model.Designer;
import com.struts2.model.Gender;
import com.struts2.model.Invoice;
import com.struts2.model.Invoice_item;
import com.struts2.model.Product_item;
import com.struts2.model.Product_line;
import com.struts2.model.Season;
import com.struts2.model.Size;
import com.struts2.model.Type;

public class ViewBuyRecordService {


	
	public List<Invoice> getInvoices(String username)
	{
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Invoice.class,"invoice");
		
		criteria.createAlias("invoice.customer", "customer");
		criteria.add(Restrictions.eq("customer.name", username));
		
		criteria.addOrder(Order.asc("status"));
		criteria.addOrder(Order.desc("date"));
		
		List<Invoice> invoice =criteria.list();
		
		session.getTransaction().commit();
		session.close();
		sF.close();
		return invoice;
	}
	
	
	public Invoice getInvoice(int id) {

		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Invoice.class);
		criteria.add(Restrictions.eq("id", id));

		Invoice invoice = (Invoice) criteria.list().get(0);

		session.getTransaction().commit();
		session.close();
		sF.close();
		return invoice;
	}
	
	
	public List<Invoice_item> getInvoiceItems(Invoice invoice) {

		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Invoice_item.class);
		criteria.add(Restrictions.eq("invoice", invoice));

		List<Invoice_item> invoice_items =  criteria.list();

		session.getTransaction().commit();
		session.close();
		sF.close();

		return invoice_items;

	}
	
	

	public void setInvoice(int id, String status, String mailing_address) {
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();

		Criteria criteria = session.createCriteria(Invoice.class);
		criteria.add(Restrictions.eq("id", id));

		Invoice invoice = (Invoice) criteria.list().get(0);

	//	System.out.print(seasonId + "\n" + id);
		// /////////////////////////////

		invoice.setMailing_address(mailing_address);
		invoice.setStatus(status);
		

		session.save(invoice);

		session.getTransaction().commit();
		session.close();
		sF.close();
	}
	
	
	
	public int getInvoiceFailCnt()
	{
		SessionFactory sF = new AnnotationConfiguration().configure()
				.buildSessionFactory();
		Session session = sF.openSession();
		// for delete , update, insert,||| not select
		session.beginTransaction();
		Criteria criteria = session.createCriteria(Invoice.class);
		criteria.add(Restrictions.eq("status", "fail"));
		
		int num = criteria.list().size();
		
		session.getTransaction().commit();
		session.close();
		sF.close();
		return num;
	}
	
	
	
	
	
}

